<!DOCTYPE html>


<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet " href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css"/>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="index.html.html">主页</a>
    <a id="isedit" href="blog_add.html">写博客</a>
    <a id="islogin" href="login.html">登录</a>

</div>
<!-- 版心 -->
<div class="container">
    <!-- 左侧个人信息 -->
    <div class="container-left">
        <div class="card">
            <img src="img/doge.jpg" class="avtar" alt="">
            <h3 id="username"></h3>
            <a href="http:www.github.com">github 地址</a>
            <div class="counter">
                <span>文章</span>
                <span>访问数</span>
            </div>
            <div class="counter">
                <span id="acount"></span>
                <span id="readcount"></span>
            </div>
        </div>
    </div>
    <!-- 右侧内容详情 -->
    <div class="container-right">
        <div class="blog-content">
            <!-- 博客标题 -->
            <h3 id="title"></h3>
            <!-- 博客时间 -->
            <div class="date">
                <span id="ctime">  </span>
            </div>
            <div id="context">
                <!-- 博客正文 -->
                <p>

                </p>
            </div>


        </div>
    </div>
</div>
<script>
    function getURLParam(key) {
        var params = location.search;//得到参数
        if (params.indexOf("?") >= 0) {//返回的字符串中有参数
            params = params.substring(1);
            var paramsArr = params.split('&');
            for (var i = 0; i < paramsArr.length; i++) {
                var namevalues = paramsArr[i].split("=");
                if (namevalues[0] == key) {
                    return namevalues[1];
                }
            }

        } else {
            return "";
        }

    }

    //1.得到id参数
    var aid = getURLParam("id");
    //进行aid 的正确性和非空判断
    if (aid == null || aid == "" || aid <= 0) {
        alert("aid参数传递错误");
        //todo:跳转到首页

    } else {


        //1.加载文章
        function loadArticle() {

            //2.根据Id得到详情
            jQuery.ajax({
                url: "artdetail",
                type: "POST",
                data: {"aid": aid},
                success: function (result) {
                    if (result != null && result.succ == 200) {
                        // 前后端交互正常
                        if (result.data != null && result.data.aid > 0) {
                            //成功查询到情况,开始拼接
                            jQuery("#title").text(result.data.title);
                            jQuery("#ctime").text(result.data.createtime);
                            jQuery("#readcount").text(result.data.readcount);
                            // jQuery("#context").text(result.data.context);
                            markdownToHtml(result.data.context);
                        } else {
                            alert("抱歉，当前用户不存在");
                        }
                    } else {
                        alert("抱歉，后端服务不可用");
                    }
                }
            });
        }

        loadArticle();

        //2加载个人信息
        function loadUserInfo() {

            // 根据ID得到个人信息
            jQuery.ajax({
                url: "getuserbyaid",
                type: "GET",
                data: {"aid": aid},
                success: function (result) {
                    if (result != null && result.succ == 200) {//前后端交互正常
                        if (result.data != null && result.data.uid > 0) {
                            //查询到了数据
                            jQuery("#username").text(result.data.username);
                            jQuery("#acount").text(result.data.acount);

                        } else {
                            alert("抱歉，后端异常" + result.msg);
                        }
                    } else {
                        alert("抱歉，后端服务有问题" + result.msg)
                    }
                }
            });

        }

        loadUserInfo();

        //3.使当前的文章阅读量+1
        function addRcount() {
            jQuery.ajax({
                url: "addrcount",
                type: "POST",
                data: {"aid": aid},
                success: function (result) {
                    if (result != null && result.succ == 200) {
                        console.log(result.data);
                    } else {
                        alert("抱歉，后端不可用");
                    }
                }
            });

        }

        addRcount();

        //判断当前是否为登陆状态
        function isLogin() {
            jQuery.ajax({
                url: "loginUserInfo",
                type: "GET",
                success: function (result) {
                    if (result != null && result.succ == 200
                        && result.data != null
                        && result.data.uid > 0) {
                        //data表示用户
                        //登录状态
                        jQuery("#isedit").attr("href", "blog_edit,html");
                        jQuery("#islogin").text("退出");
                        jQuery("#islogin").attr("href", "javascript:logout()");

                    } else {
                        //非登录状态
                        jQuery("#isedit").attr("href", "login.html");
                        jQuery("#islogin").text("登录");
                        jQuery("#islogin").attr("href", "login.html");
                    }
                }

            });
        }

        isLogin();

        //退出时间
        function logout() {
            if (confirm("是否退出")) {
                //1.清空session
                jQuery.ajax({
                    url: "loginout",
                    type: "POST",
                    success: function (result) {
                        if (result != null && result.succ == 200 && result.data == 1) {
                            alert("恭喜，退出成功");
                            //这里注意页面跳转的格式
                            // location.href("login.html");
                            location.href = location.href;//刷新当前页面
                        } else {
                            alert("抱歉，操作出错，请重试");
                        }
                    }
                });

            }

        }


    }


    //将Markdown转换成Html
    function markdownToHtml(md) {

        editormd.markdownToHTML("context", {
            markdown : md, // Also, you can dynamic set Markdown text
            // htmlDecode : true,  // Enable / disable HTML tag encode.
            // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
        });
    }


</script>

</body>

</html>